//
//  ActDetailListTableViewController.m
//  TravelFirst
//
//  Created by lanou3g on 15/9/21.
//  Copyright (c) 2015年 吴娅君. All rights reserved.
//

#import "ActDetailListTableViewController.h"
#import "ActListHeaderView.h"
#import "ActListDetailTableViewCell.h"
#import "ActListDetailModel.h"
#import "ActDetailViewController.h"


@interface ActDetailListTableViewController ()
//声明一个装轮播图列表model的数组
@property (nonatomic,strong) NSMutableArray *CycleArr;

@end

@implementation ActDetailListTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self jsonCycleList];
    self.tableView.separatorStyle = 0;
    [self.tableView registerClass:[ActListDetailTableViewCell class] forCellReuseIdentifier:@"ListCell"];
}

- (NSMutableArray *)CycleArr{
    if(!_CycleArr){
        self.CycleArr = [NSMutableArray array];
    }
    return _CycleArr;
}

//解析轮播图列表
- (void)jsonCycleList{
    [LORequestManger GET:self.CycleUrl success:^(id response) {
        NSArray *mdic = (NSArray *)response;
        for (int i = 0; i < mdic.count; i++) {
            ActListDetailModel *model = [[ActListDetailModel alloc] init];
            NSDictionary *dic = mdic[i][0];
            [model setValuesForKeysWithDictionary:dic];
            [self.CycleArr addObject:model];
        }
        [self setHeader];
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}

-(void)setHeader{
    ActListHeaderView *headerView = [[ActListHeaderView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 180) pic:_picUrl];
    self.tableView.tableHeaderView=headerView;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.CycleArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ActListDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ListCell" forIndexPath:indexPath];
    ActListDetailModel *model = self.CycleArr[indexPath.row];
    [cell.pictuer sd_setImageWithURL:[NSURL URLWithString:model.cover]];
    cell.title.text = model.title;
    cell.address.text = [NSString stringWithFormat:@"[%@]",model.destination];
    [cell.logo sd_setImageWithURL:[NSURL URLWithString:model.club[@"logo"]]];
    cell.logoTitle.text = model.club[@"title"];
    cell.time.text = model.period_desc;
    cell.day.text = [NSString stringWithFormat:@"行程%ld天",model.days];
    cell.date.text = [self Date:model.start_time];
    cell.money.text = [NSString stringWithFormat:@"￥%@-￥%@",model.min_cost,model.max_cost];
    return cell;
}

//将时间戳转化为时间
- (NSString *)Date:(NSString *)str{
    NSTimeInterval time=[str doubleValue]+28800;//因为时差问题要加8小时 == 28800 sec
     NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"MM月dd日"];
    NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];
    return currentDateStr;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ActDetailViewController *con = [[ActDetailViewController alloc] init];
    ActListDetailModel *model = self.CycleArr[indexPath.row];
    NSString *url = [NSString stringWithFormat:@"http://tubu.ibuzhai.com/rest/v3/activity/%@?access_token=1d209720-f920-497e-8faf-e0e8bc7fbb0a&app_version=2.3.6&device_type=1",model.ID];
    con.detailUrl = url;
    con.day = model.days;
    [self.navigationController pushViewController:con animated:NO];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return kWidth/1.3;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
